<?php
/*
	[UCenter Home] (C) 2007-2008 Comsenz Inc.
	@author:tong
	@company:重庆致晓网络
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}

$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_moviecredit')." WHERE type='movie'");
$value = $_SGLOBAL['db']->fetch_array($query);
$_SGLOBAL['kanmovie']=$value['kanmovie'];

//去除URL里有UID就认为VIEW=ME；
if ($_GET['uid']) {	
	$_GET['view']='all';
}
//获取电影ID
$moveid = empty($_GET['id'])?0:intval($_GET['id']);

$sourceid = empty($_GET['sourceid'])?0:intval($_GET['sourceid']);


//获取页数
$page = empty($_GET['page'])?1:intval($_GET['page']);
//地域分类
$all_actives = array($_GET['regionid']=>' class="current"');

if($page<1) $page=1;

//登陆用户信息
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('space')." WHERE uid='$_SGLOBAL[supe_uid]' LIMIT 1");
$mexinxi = $_SGLOBAL['db']->fetch_array($query);

//最热电影
$wheresqlhot = 'checked=1';
if ($_GET[view]) {
	if ($_GET[view]!='all'&&$_GET[view]!='me'&&$_GET[view]!='keep') {
		$vew = intval($_GET[view]);
		$wheresqlhot .=" AND typeid='$vew'"; 
	}
	if ($_GET[view]=='me'||$_GET[view]=='keep') {
		$wheresqlhot .=" AND uid='$_SGLOBAL[supe_uid]'"; 
	}

}
if ($_GET[view]=='kan'||$_GET[view]=='show') {
	$wheresqlhot = 'checked=1';
}
$query = $_SGLOBAL['db']->query("SELECT movieid,mname,dateline FROM ".tname('app_movie')." WHERE $wheresqlhot ORDER BY times DESC LIMIT 8");
$hotmovie = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$hotmovie[] = $value;
}
//var_dump($hotmovie);

//观看最多的用户
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_kanusers')." ORDER BY kannum DESC LIMIT 3");
$kanuser = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$kanuser[] = $value;
}
//var_dump($kanuser);


//发布最多的用户
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_sendusers')." ORDER BY sendnum DESC LIMIT 3");
$senduser = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$senduser[] = $value;
}
//var_dump($senduser);

//评分最高的
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." ORDER BY (score/scorenum) DESC LIMIT 6");
$fenmovie = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$value[fscore] = round($value[score]/$value[scorenum],1);
	$fenmovie[] = $value;
}
//地域分类
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_region')." ORDER BY sort ASC");
$regionlist = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$regionlist[] = $value;
}
//var_dump($regionlist);

//电影分类
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_type')." ORDER BY sort ASC");
$typelist = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$typelist[] = $value;
}
//var_dump($typelist);
//看过此用户的人
$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_kanuser')." WHERE movieid='$moveid' ORDER BY dateline DESC LIMIT 0,6");
$kanusers = array();
while ($value = $_SGLOBAL['db']->fetch_array($query)) {
	$kanusers[] = $value;
}
//var_dump($kanusers);


if($moveid) {
	//显示电影详细信息；观看电影$_GET[view]==kan;
	//$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." AS movie RIGHT JOIN ".tname('app_movieurl')." AS url ON(movie.movieid=url.movieid) WHERE movie.movieid='$moveid' LIMIT 1");
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." WHERE movieid='$moveid' LIMIT 1");
	$movieshow = $_SGLOBAL['db']->fetch_array($query);
	
	//播放器
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('topic')." WHERE target='movie' AND tarid='$moveid' LIMIT 1");
	$movietopic = $_SGLOBAL['db']->fetch_array($query);
	$topicid=$movieshow['topicid']; 
	//影评话题	cp.php?ac=blog&topicid=2
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_plays')." ");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) { 
		$players[$value['playid']]=$value;
		//$movieshow['playcode'] = $value[playercontent];
		//$movieshow['playername'] = $value[playername];
	}
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movieurl')." WHERE movieid='$moveid' ");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) { 
		$sid=$value['sourceid'];
		//$movieshow['playcode'][$sid]=$players[$value['playid']]['playercontent'];
		$movieshow['playername'][$sid]=$players[$value['playid']]['playername']; 
			//$movieshow['movieurl'][$url]=$value;  
			if (strpos(trim($value['movieurl'],'$'),"$")) {
				$soruces = explode("\n",$value['movieurl']);
				$movieurl = array();
				$i = 0;
				foreach ($soruces as $key => $value){
					preg_match_all ("/(.*?)\\$(.*)/", trim($value), $matches);
					
					$movieurl[$matches[1][0]] = $matches[2][0]; 
					$i++;	//var_dump($matches);
				}
				$movieshow['jiming'][$sid] = 1; 
				$movieshow['movieurl'][$sid] = $movieurl;
				
			}else { 
				$movieshow['jiming'][$sid] = 0; 
				$movieshow['movieurl'][$sid] = explode("\n",$value['movieurl']); 
			} 
			$url++;  
	} 
	//添加评分
	if ($movieshow[scorenum]) {
		$score = round($movieshow[score]/$movieshow[scorenum],1);
	}else {
		$score = 0;
	}
	$movieshow[fen] = $score;
	//echo $data[score].'|'.$data[scorenum];exit();
	if ($score<2) {
		$movieshow['class'] = 'score';
	}if ($score==2) {
		$movieshow['class'] = 'score score1';
	}
	if ($score>2&&$score<=4) {
		$movieshow['class'] = 'score score2';
	}
	if ($score>4&&$score<=6) {
		$movieshow['class'] = 'score score3';
	}
	if ($score>6&&$score<=8) {
		$movieshow['class'] = 'score score4';
	}
	if ($score>8&&$score<=10) {
		$movieshow['class'] = 'score score5';
	}
	
	$movieshow['images'] =  $movieshow['images']?$movieshow['images']:'image/nomoviepic.gif';
	//集数显示
	//var_dump($movieshow[movieurl]);exit();
	//类型
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_type')." WHERE typeid='$movieshow[typeid]' LIMIT 1");
	$value = $_SGLOBAL['db']->fetch_array($query);
	$movieshow['type'] = $value[typename];
	//var_dump($movieshow[typeid]);
	$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_region')." WHERE regionid='$movieshow[regionid]' LIMIT 1");
	$value = $_SGLOBAL['db']->fetch_array($query);
	$movieshow['region'] = $value[regionname];
	//var_dump($_GET['part']);exit();
	//播放信息；
	if ($_GET[view]=='kan') {
		if ($_SGLOBAL[supe_uid]=='') {
			showmessage('对不起你还未登陆','index.php');exit();
		}
		
		if ($space[credit] < $_SGLOBAL['kanmovie']) {
			showmessage('对不起你的积分不够用来观看此电影','index.php');exit();
		}
	
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movieurl')." Where sourceid='$sourceid' LIMIT 1");
		//$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." WHERE movieid='$moveid' LIMIT 1");
		$value = $_SGLOBAL['db']->fetch_array($query);
		$movieshow['playcode'] = $players[$value['playid']]['playercontent'] ; 
			if (strpos(trim($value['movieurl'],'$'),"$")) {
				$movieshow['sourceurl'] = explode("\n",$value['movieurl']);
				$movieurl = array();
				$i = 0;
				foreach ($movieshow['sourceurl'] as $key => $value){
					preg_match_all ("/(.*?)\\$(.*)/", trim($value), $matches);
					$movieurl[$matches[1][0]] = $matches[2][0]; 
					$i++;	//var_dump($matches);
				}
				$movieshow['sourceurl'] = $movieurl;
				//print_r($movieshow['sourceurl']);
				$movieshow['jiming'] =1;
			}else {
				$movieshow['jiming'] =0;
				$movieshow['sourceurl'] = explode("\n",$value['movieurl']); 
			}  
		$part = $_GET['part']?$_GET['part']:0;
		$url = trim($movieshow['sourceurl'][$part]);
		$movieshow['playcode'] = str_replace('$url',$url,$movieshow['playcode']);
		//$_SGLOBAL['db']->query("UPDATE ".tname('app_movie')." SET times=times+1 WHERE movieid='$moveid'"); 
		if( $_SCOOKIE['view_movieid'] != $moveid && $movieshow['checked']==1) {
			//更新观看次数
			$_SGLOBAL['db']->query("UPDATE ".tname('app_movie')." SET times=times+1 WHERE movieid='$moveid'");
			//更新用户观看次数
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_kanusers')." WHERE uid='$_SGLOBAL[supe_uid]'");
			if ($moviekan=$_SGLOBAL['db']->fetch_array($query)) {
				$_SGLOBAL['db']->query("UPDATE ".tname('app_movie_kanusers')." SET kannum=kannum+1 WHERE uid='$_SGLOBAL[supe_uid]'");
			}else {
				$_SGLOBAL['db']->query("INSERT INTO ".tname('app_movie_kanusers')." VALUES('','$_SGLOBAL[supe_uid]','$_SGLOBAL[supe_username]','1')");
			}
			
			//更新最近观看的人；
			$now = time();
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_kanuser')." WHERE uid='$_SGLOBAL[supe_uid]' AND movieid='$moveid'");
			if ($moviekan=$_SGLOBAL['db']->fetch_array($query)) {
				$_SGLOBAL['db']->query("UPDATE ".tname('app_movie_kanuser')." SET times=times+1,dateline='$now' WHERE uid='$_SGLOBAL[supe_uid]' AND movieid='$moveid'");
			}else {
				$_SGLOBAL['db']->query("INSERT INTO ".tname('app_movie_kanuser')." VALUES('','$moveid','$_SGLOBAL[supe_uid]','$_SGLOBAL[supe_username]','$now','1')");
				
				//第一次观看为作者添加积分；
				$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit+$_SGLOBAL[kanmovie] WHERE uid='$movieshow[uid]'");
				
				//第一次观看消费积分；
				$_SGLOBAL['db']->query("UPDATE ".tname('space')." SET credit=credit-$_SGLOBAL[kanmovie] WHERE uid='$_SGLOBAL[supe_uid]'");
			}
			
			inserttable('log', array('id'=>$space['uid'], 'idtype'=>'uid'));//延迟更新
			ssetcookie('view_movieid', $moveid);
		}
		$fs = array();
		$fs['icon'] = 'video';
	    $fs['title_template'] = "{actor} 正在观看影片 {subject} ";
		
		$fs['title_data'] = array(
			'subject' => "<a href=\"space.php?do=movie&id={$movieshow['movieid']}\" target=_blank>{$movieshow['mname']}</a>"
		);
		$fs['body_template'] = '';
		$fs['id'] = $movieid;
		$fs['idtype'] = 'movieid';
		$fs['body_data'] = array();
		
		if ($movieshow['checked']==1) {
			include_once(S_ROOT.'./source/function_cp.php');
			$feedid = feed_add($fs['icon'], $fs['title_template'], $fs['title_data'], $fs['body_template'], $fs['body_data'], $fs['body_general'],$fs['images'], $fs['image_links'], $fs['target_ids'], $fs['friend'],'','1');
			//var_dump($feedid. $_GET[id]);exit();
			if ($feedid&&$_GET[id]) {
				$sql = "UPDATE ".tname('feed')." SET id = ".$_GET[id].",idtype='movieid' WHERE feedid =".$feedid;
				$_SGLOBAL['db']->query($sql);
			}
		}
		
	}
	
	//评论
	$perpage = 10;
	$perpage = mob_perpage($perpage);
	
	$start = ($page-1)*$perpage;

	//检查开始数
	ckstart($start, $perpage);

	$count = $movieshow['replynum'];

	$list = array();
	if($count) {
		$cid = empty($_GET['cid'])?0:intval($_GET['cid']);
		$csql = $cid?"cid='$cid' AND":'';

		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('comment')." WHERE $csql id='$moveid' AND idtype='movieid' ORDER BY dateline LIMIT $start,$perpage");
		while ($value = $_SGLOBAL['db']->fetch_array($query)) {
			realname_set($value['authorid'], $value['author']);//实名
			$list[] = $value;
		}
	}

	//分页
	$multi = multi($count, $perpage, $page, "space.php?uid=$movieshow[uid]&do=$do&id=$moveid", '', 'content');
	//var_dump($list);exit();
	
	
	//作者另外的影片
	$query = $_SGLOBAL['db']->query("SELECT * FROM (SELECT * FROM ".tname('app_movie')." WHERE movieid!='$moveid' AND uid='$movieshow[uid]') AS movie WHERE movie.checked=1 ORDER BY dateline DESC LIMIT 0,8");
	while ($value = $_SGLOBAL['db']->fetch_array($query)) {
		$otherlist[] = $value;
	}
	//var_dump($otherlist);
	
	if ($_GET[view]=='invite') {
		//var_dump($_GET[view]);
		if(submitcheck('invitesubmit')){
			//var_dump($_POST);exit();
			foreach ($_POST[ids] as $value){
				include_once(S_ROOT.'./source/function_cp.php');
				$message = "邀请您观看电影 <a href=\"space.php?do=movie&id={$_GET[id]}\">{$movieshow[mname]}</a>";
			    notification_add($value['fuid'], "app_movie", $message );
			}
			showmessage("do_success", "space.php?do=movie&view=invite&id=$_GET[id]&page=$_GET[page]", 2);
		}
	
		//分页
		$perpage = 21;
		$page = empty($_GET['page'])?0:intval($_GET['page']);
		if($page<1) $page = 1;
		$start = ($page-1)*$perpage;
		//检查开始数
		ckstart($start, $perpage);
	
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('friend')." WHERE uid='$_SGLOBAL[supe_uid]' AND status='1'"), 0);
	
		$fuids = array();
		$list = array();
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('friend')." WHERE uid='$_SGLOBAL[supe_uid]' AND status='1' ORDER BY num DESC, dateline DESC LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				realname_set($value['fuid'], $value['fusername']);
				$list[] = $value;
				//$fuids[] = $value['fuid'];
			}
		}
		//var_dump($list);
		$multi = multi($count, $perpage, $page, "space.php?do=movie&view=invite&id=$_GET[id]");
	}
	
	$_TPL['css'] = 'album';
	include_once template("space_video_view");

} else {
	//列表
	$perpage = 16;
	$perpage = mob_perpage($perpage);
	
	$start = ($page-1)*$perpage;
	
	//检查开始数
	ckstart($start, $perpage);

	if(empty($_GET['view'])) {
		$_GET['view'] = 'all';//默认显示
	}
	//var_dump($_GET['view']);
	if($_GET['view'] == 'all') {
		$actives = array('all'=>' class="active"');
		if ($_GET['regionid']||$_GET['keywords']||$_GET['uid']) {
			$uid = intval($_GET['uid']);
			$regionid = intval($_GET['regionid']);
			$keywords = $_GET['keywords'];
			$wheresql = 'checked=1';
			if ($uid) {
				$wheresql .=" AND uid='$uid'"; 
			}
			if ($regionid) {
				$wheresql .=" AND regionid='$regionid'";
			}
			if ($keywords) {
				$wheresql .=" AND mname like '%$keywords%'";
			}
			
			$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('app_movie')." WHERE $wheresql"),0);
			$list = array();
			if($count) {
				$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." WHERE $wheresql ORDER BY dateline DESC LIMIT $start,$perpage");
				while ($value = $_SGLOBAL['db']->fetch_array($query)) {
					$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
					$list[] = $value;
				}
			}
			
			//分页
			//var_dump($theurl);
			$urlkeywords = urlencode($keywords);
			$theurl = "space.php?do=$do&view=all&uid=$uid&regionid=$regionid&keywords=$urlkeywords";
			$multi = multi($count, $perpage, $page, $theurl);
			//var_dump($list);
		}else {
			$wheresql = 'checked=1';
			//最热电影4
			$query = $_SGLOBAL['db']->query("SELECT uid,movieid,mname,images,username,dateline,times FROM ".tname('app_movie')." WHERE $wheresql ORDER BY times DESC LIMIT 8");
			$indexhot = array();
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
				$indexhot[] = $value;
			}
			//最新上线4
			$query = $_SGLOBAL['db']->query("SELECT uid,movieid,mname,images,username,dateline FROM ".tname('app_movie')." WHERE $wheresql ORDER BY dateline DESC LIMIT 8");
			$indexnew = array();
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
				$indexnew[] = $value;
			}
		}
	} elseif ($_GET['view'] == 'me') {
		$actives = array($_GET['view']=>' class="active"');
		$wheresql = 1;
		if ($_SGLOBAL['supe_uid']) {
			$wheresql .= " AND uid='$_SGLOBAL[supe_uid]'";
			//var_dump($wheresql);
		}
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('app_movie')." WHERE $wheresql"),0);
		$list = array();
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." WHERE $wheresql ORDER BY dateline DESC LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
				$list[] = $value;
			}
		}
		//发布最多的用户
		$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_sendusers')." WHERE $wheresql LIMIT 1");
		$mesend = $_SGLOBAL['db']->fetch_array($query);
		
		//分页
		//var_dump($theurl);
		$theurl = "space.php?do=$do&view=me";
		$multi = multi($count, $perpage, $page, $theurl);
		//var_dump($list);
		
		
	}elseif($_GET['view'] == 'keep'){
		if(!empty($_GET['keepid'])){
			$movieid = intval($_GET['keepid']);
			$_SGLOBAL['db']->query("DELETE FROM ".tname('app_movie_keep')." WHERE uid='$_SGLOBAL[supe_uid]' AND movieid='$movieid'");
			showmessage('删除收藏成功','space.php?do=movie&view=keep');
			exit();
		}
		$wheresql = 1;
		if ($_SGLOBAL['supe_uid']) {
			$wheresql .= " AND keep.uid='$_SGLOBAL[supe_uid]'";
			//var_dump($wheresql);
		}
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('app_movie_keep')." AS keep WHERE $wheresql"),0);
		$list = array();
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie_keep')." AS keep RIGHT JOIN ".tname('app_movie')." AS movie ON(keep.movieid=movie.movieid) WHERE $wheresql ORDER BY dateline DESC LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
				$list[] = $value;
			}
		}
		//发布最多的用户
		
		//分页
		//var_dump($theurl);
		$theurl = "space.php?do=$do&view=keep";
		$multi = multi($count, $perpage, $page, $theurl);
		//var_dump($list);
	}else {
		$actives = array($_GET['view']=>' class="active"');
		$wheresql = "checked=1";
		if ($_GET['view']) {
			$view = intval($_GET['view']);
			$wheresql .= " AND typeid='$view'";
		}
		if ($_GET['regionid']) {
			$regionid = intval($_GET['regionid']);
			$wheresql .=" AND regionid='$regionid'";
		}
		$count = $_SGLOBAL['db']->result($_SGLOBAL['db']->query("SELECT COUNT(*) FROM ".tname('app_movie')." WHERE $wheresql"),0);
		$list = array();
		if($count) {
			$query = $_SGLOBAL['db']->query("SELECT * FROM ".tname('app_movie')." WHERE $wheresql ORDER BY dateline DESC LIMIT $start,$perpage");
			while ($value = $_SGLOBAL['db']->fetch_array($query)) {
				$value['images'] = $value['images']?$value['images']:'image/nomoviepic.gif';
				$list[] = $value;
			}
		}
		
		//分页
		//var_dump($theurl);
		$theurl = "space.php?do=$do&view=$view&regionid=$regionid";
		$multi = multi($count, $perpage, $page, $theurl);
	//var_dump($actives);
	}
	//实名
	realname_get();

	$_TPL['css'] = 'album';
	include_once template("space_video_list");
} 
?>